package jsyf.oa.service.system;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.constant.WechatConstant;
import jsyf.com.util.DateUtils;
import jsyf.oa.dao.system.UserDao;
import jsyf.oa.dao.system.WechatShareDetailDao;
import jsyf.oa.entity.system.WechatShareDetailEntity;
import jsyf.oa.facade.system.WechatShareDetailFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;

/**
 * Created by WHH-PC on 2018/1/12.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class WechatShareDetailService implements WechatShareDetailFacade {

    @Autowired
    private WechatShareDetailDao wechatShareDetailDao;
    @Autowired
    private UserDao userDao;

    @Override
    public String save(String jsonData) {
        WechatShareDetailEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), WechatShareDetailEntity.class);
        entity.setCreateUserId(entity.getUserId());
        JSONObject userJson = userDao.getViewJSONById(entity.getUserId());
        entity.setDelFlag(1);
        if(userJson != null){
            entity.setCompanyId(userJson.get("companyId").toString());
            //大区经理和区域经理转发为有效转发
            if("1".equals(userJson.getString("frontFlag"))){
                //判断是否是有效时间的分享
                Date createTime =  wechatShareDetailDao.getCurrentTime();
                if( DateUtils.isInDate(createTime, WechatConstant.nightStart, WechatConstant.nightEnd)){
                    entity.setDelFlag(0);
                }
            }
        }
        //JSONObject jsonObject=wechatShareDetailDao.checkIsSave(entity.getUserId(),entity.getWechatShareId());
        //if(jsonObject==null){
            wechatShareDetailDao.add(entity);
       // }
        return entity.getId();
    }

}
